<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%java.util.Random r=new java.util.Random();  %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/calendar.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function showHidden(id){
		var obj = document.getElementById(id);
		if(obj.style.display == "none"){
			document.getElementById(id).style.display = "block";
			document.getElementById("img"+id).src = "./images/nolines_minus.gif";
		}else{
			document.getElementById(id).style.display = "none";
			document.getElementById("img"+id).src = "./images/nolines_plus.gif";
		}
	}
	
	function checkForm(){
		var deptNames = document.getElementById("deptNames");
		var startDate = document.getElementById("startDate");
		var endDate = document.getElementById("endDate");
		
		if(deptNames.value == ''){
			alert("选择分析对象！");
			return false;
		}
		
		if(startDate.value == ''){
			alert("请选择开始日期！");
			return false;
		}
		if(endDate.value == ''){
			alert("请选择截止日期！");
			return false;
		}
		if(startDate.value != ''){
			if(endDate.value == ''){
				alert("请选择截止日期！");
				return false;
			}
		}
		if(endDate.value != ''){
			if(startDate.value == ''){
				alert("请选择开始日期！");
				return false;
			}
		}
		if(startDate.value != '' && endDate.value != ''){
			var d1 = (startDate.value).replace("-","/");
			var d2 = (endDate.value).replace("-","/");
			if(Date.parse(d1) - Date.parse(d2) >= 0){
				alert("截止日期不能小于或等于开始日期，请重新选择日期！");
				return false;
			}
		}
		return true;
		
	}
	
	function back(){
		window.location.href = "./manageReport.do";
		window.location.target = "main";
	}
	
	function deptDialog(){
		var url = "./manageReport.do?action=selectDept&radom="+<%=r.nextInt(100)%>;
		var dlgParam = "dialogHeight:350px;dialogWidth:300px;dialogTop:300px;dialogLeft:450px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		
		if(rt != undefined){
			//根据从子窗口得到部门ids和部门名称字符串
			var values = rt.split("#");
			if(values[0] != "" && values[1] != ""){
				document.getElementById("deptIds").value = values[0];
				document.getElementById("deptNames").value = values[1];
			}
		}
	}
	
	function createReport(){
		if(checkForm()){
			document.salePeriodReportForm.action = "./manageReport.do?action=salePeriodReport";
			document.salePeriodReportForm.target = "main";
			document.salePeriodReportForm.submit();
		}
	}
	function aaa(aaa){
		window.location.href = aaa;
		window.location.target = "main";
		}
-->
</script>
</head>
<body>
<form name="salePeriodReportForm" action="" method="post">
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table cellpadding="0" cellspacing="0" width="100%" height="100%">
								<tr>
									<td class="title_lab">分析报表</td>
									<td align="left">&nbsp;&nbsp;快速查看：
									<select onchange="aaa(this.options[this.selectedIndex].value);">
									<option>-------------请选择-------------</option>
									<option value="./manageReport.do?action=preSalePeriodReport">销售周期分析表</option>
									<option value="./manageReport.do?action=saleMonthReport">销售月度统计表</option>
									<option value="./manageReport.do?action=allCustomerDevoteAnalysis">客户贡献统计表</option>
									<option value="./manageReport.do?action=oldCustomerDevoteAnalysis">老客户贡献分析表</option>
									<option value="./manageReport.do?action=marketExpandsAnalysis">市场开拓同步分析表</option>
									<option value="./manageReport.do?action=annualWreathCompares">年度环比分析表</option>
									<option value="./manageReport.do?action=getLinkManMailInfo">联系人邮件列表</option>
									<option value="./manageReport.do?action=CustomerStatisticContract">按客户统计合同</option>
									<option value="./manageReport.do?action=saleStatisticContract">按销售统计合同</option>
									
									</select>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr><td colspan="3" height="50" style="height: 55px;text-align: center;font-weight: bold;font-size: 24px;">销售周期分析表</td></tr>
					<tr>
						<td colspan="3" align="center">
							<table class="table_report1" cellpadding="0" cellspacing="0" height="100%" style="width: 95%;">
								<c:choose>
								<c:when test="${isViewDeptManageReport == 1 || isViewAllManageReport == 1}">
								<tr>
									<td align="right">请选择分析对象：</td>
									<td align="left">
										<input id="deptNames" name="deptNames" class="input_report1" type="text" value="${manageReport.deptNames}"/>
										<input id="deptIds" name="deptIds" type="hidden" value="${manageReport.deptIds}"/>
										<input class="btn_select" type="button" value="" onclick="deptDialog()"/>
									</td>
								</tr>
								</c:when>
								<c:otherwise>
								<tr>
									<td align="right">分析对象：</td>
									<td align="left">${userName}
										<input id="deptNames" name="deptNames" type="hidden" value="${userName}"/>
										<input id="userId" name="userId" type="hidden" value="${userId}"/>
									</td>
								</tr>
								</c:otherwise>
								</c:choose>
								<tr>
									<td align="right">开始时间：</td>
									<td align="left">
										<input id="startDate" name="startDate" class="input_report2" type="text" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly" 
											value="<fmt:formatDate value='${manageReport.startDate}' pattern='yyyy-MM-dd'/>"/>
										到
										<input id="endDate" name="endDate" class="input_report2" type="text" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly" 
											value="<fmt:formatDate value='${manageReport.endDate}' pattern='yyyy-MM-dd'/>"/>
									</td>
								</tr>
								<tr>
									<td colspan="2" align="center" height="30">
										<input class="btn_report" type="button" value="" onclick="createReport()"/>&nbsp;&nbsp;
										<button class="btn_back" onclick="back()" hidefocus="true">返回</button>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr onclick="showHidden('1')">
						<td colspan="3" class="title_info">
							<img id="img1" src="./images/nolines_minus.gif">业绩达成状态
						</td>
					</tr>
					<tr id="1">
						<td colspan="3" align="center">
							<table class="table_view" cellpadding="0" cellspacing="0" height="100%">
								<tr>
									<td class="title">部门</td>
									<td class="title">销售员</td>
									<td class="title">周期业绩指标</td>
									<td class="title">周期完成金额</td>
									<td class="title">周期完成比例</td>
									<td class="title">周期回款金额</td>
									<td class="title">应收账款</td>
									<td class="title">周期费用申请</td>
									<td class="title">周期预测签约金额</td>
									<td class="title">周期实际签约金额</td>
								</tr>
								<c:forEach var="department" items="${salePeriodReport['departments']}">
								
								<c:forEach var="user" items="${department.users}" begin="0" end="0">
								<tr>
									<td rowspan="${fn:length(department.users) + 1}">${department.deptName}</td>
									<td>${user.userName}</td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.businessTarget}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.finishCost}" pattern="###,###,###,###.##"/></td>
									<td><fmt:formatNumber value="${user.salePeriodReport.finishRate}" pattern="############.##"/>%</td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.backCost}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.receivable}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.saleCost}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.predictCost}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.contractCost}" pattern="###,###,###,###.##"/></td>
								</tr>
								</c:forEach>
								<c:forEach var="user" items="${department.users}" begin="1">
								<tr>
									<td>${user.userName}</td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.businessTarget}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.finishCost}" pattern="###,###,###,###.##"/></td>
									<td><fmt:formatNumber value="${user.salePeriodReport.finishRate}" pattern="############.##"/>%</td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.backCost}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.receivable}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.saleCost}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.predictCost}" pattern="###,###,###,###.##"/></td>
									<td>￥<fmt:formatNumber value="${user.salePeriodReport.contractCost}" pattern="###,###,###,###.##"/></td>
								</tr>
								</c:forEach>
								<tr>
									<td class="title3">小计：</td>
									<td class="title3">￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.businessTarget}" pattern="###,###,###,###.##"/></td>
									<td class="title3">￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.finishCost}" pattern="###,###,###,###.##"/></td>
									<td class="title3"><fmt:formatNumber value="${department.smallTotalSalePeriodReport.finishRate}" pattern="############.##"/>%</td>
									<td class="title3">￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.backCost}" pattern="###,###,###,###.##"/></td>
									<td class="title3">￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.receivable}" pattern="###,###,###,###.##"/></td>
									<td class="title3">￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.saleCost}" pattern="###,###,###,###.##"/></td>
									<td class="title3">￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.predictCost}" pattern="###,###,###,###.##"/></td>
									<td class="title3">￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.contractCost}" pattern="###,###,###,###.##"/></td>
								</tr>
								</c:forEach>
								<tr>
									<td class="title4" align="center" colspan="2">合计：</td>
									<td class="title4" style="color: red;">￥<fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].businessTarget}" pattern="###,###,###,###.##"/></td>
									<td class="title4" style="color: red;">￥<fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].finishCost}" pattern="###,###,###,###.##"/></td>
									<td class="title4" style="color: red;"><fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].finishRate}" pattern="############.##"/>%</td>
									<td class="title4" style="color: red;">￥<fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].backCost}" pattern="###,###,###,###.##"/></td>
									<td class="title4" style="color: red;">￥<fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].receivable}" pattern="###,###,###,###.##"/></td>
									<td class="title4" style="color: red;">￥<fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].saleCost}" pattern="###,###,###,###.##"/></td>
									<td class="title4" style="color: red;">￥<fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].predictCost}" pattern="###,###,###,###.##"/></td>
									<td class="title4" style="color: red;">￥<fmt:formatNumber value="${salePeriodReport['totalSalePeriodReport'].contractCost}" pattern="###,###,###,###.##"/></td>
								</tr>
							</table><br>
						</td>
					</tr>
					<tr onclick="showHidden('4')">
						<td colspan="3" class="title_info">
							<img id="img4" src="./images/nolines_minus.gif">本周期新增商机
						</td>
					</tr>
					<tr id="4">
						<td colspan="3" align="center">
							<table class="table_view" cellpadding="0" cellspacing="0">
								<tr>
									<td class="title">部门</td>
									<td class="title">销售员</td>
									<td class="title">序号</td>
									<td class="title">客户名称</td>
									<td class="title">来源</td>
									<td class="title">预测签约时间</td>
									<td class="title">预测金额</td>
									<td class="title">需求描述</td>
									<td class="title">销售阶段</td>
									<td class="title">本周期动作</td>
								</tr>
								<c:forEach var="department" items="${salePeriodReport4['departments']}">
								
								<c:forEach var="user" items="${department.users}" begin="0" end="0">
								
								<c:forEach var="customer" items="${user.customers}" begin="0" end="0" varStatus="c">
								<tr>
									<td rowspan="${fn:length(department.users) + department.smallTotalSalePeriodReport.customerNum + 1}">${department.deptName}</td>
									<td rowspan="${fn:length(user.customers) + 1}">${user.userName}</td>
									<td>${c.count}</td>
									<td>${customer.customerName}</td>
									<td>
										<c:choose>
											<c:when test="${customer.customerSourceId == 1}"><fmt:message key='customer.select.self'/></c:when>
											<c:when test="${customer.customerSourceId == 2}"><fmt:message key='customer.select.companySet'/></c:when>
											<c:when test="${customer.customerSourceId == 3}"><fmt:message key='customer.select.market'/></c:when>
											<c:otherwise><fmt:message key='customer.select.other'/></c:otherwise>
										</c:choose>
									</td>
									<td><fmt:formatDate value="${customer.predictContract}" pattern="yyyy-MM-dd"/></td>
									<td>￥<fmt:formatNumber value="${customer.predictCost}" pattern="###,###,###,###.##"/></td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.requireDesc) > 8}">${fn:substring(customer.requireDesc,0,8)}......</c:when>
											<c:otherwise>${customer.requireDesc}</c:otherwise>
										</c:choose> 
									</td>
									<td>${customer.saleStage}</td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.saleRecord.subject) > 8}">${fn:substring(customer.saleRecord.subject,0,8)}......</c:when>
											<c:otherwise>${customer.saleRecord.subject}</c:otherwise>
										</c:choose> 
									</td>
								</tr>
								</c:forEach>
								<c:forEach var="customer" items="${user.customers}" begin="1" varStatus="c">
								<tr>
									<td>${c.count + 1}</td>
									<td>${customer.customerName}</td>
									<td>
										<c:choose>
											<c:when test="${customer.customerSourceId == 1}"><fmt:message key='customer.select.self'/></c:when>
											<c:when test="${customer.customerSourceId == 2}"><fmt:message key='customer.select.companySet'/></c:when>
											<c:when test="${customer.customerSourceId == 3}"><fmt:message key='customer.select.market'/></c:when>
											<c:otherwise><fmt:message key='customer.select.other'/></c:otherwise>
										</c:choose>
									</td>
									<td><fmt:formatDate value="${customer.predictContract}" pattern="yyyy-MM-dd"/></td>
									<td>￥<fmt:formatNumber value="${customer.predictCost}" pattern="###,###,###,###.##"/></td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.requireDesc) > 8}">${fn:substring(customer.requireDesc,0,8)}......</c:when>
											<c:otherwise>${customer.requireDesc}</c:otherwise>
										</c:choose> 
									</td>
									<td>${customer.saleStage}</td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.saleRecord.subject) > 8}">${fn:substring(customer.saleRecord.subject,0,8)}......</c:when>
											<c:otherwise>${customer.saleRecord.subject}</c:otherwise>
										</c:choose> 
									</td>
								</tr>
								</c:forEach>
								<tr>
									<td class="title5">小计：</td>
									<td class="title5">客户数量：${fn:length(user.customers)}个</td>
									<td class="title5" colspan="6">预测签约金额：￥<fmt:formatNumber value="${user.salePeriodReport.predictCost}" pattern="###,###,###,###.##"/></td>
								</tr>
								</c:forEach>
								<c:forEach var="user" items="${department.users}" begin="1">
								
								<c:forEach var="customer" items="${user.customers}" begin="0" end="0" varStatus="c">
								<tr>
									<td rowspan="${fn:length(user.customers) + 1}">${user.userName}</td>
									<td>${c.count}</td>
									<td>${customer.customerName}</td>
									<td>
										<c:choose>
											<c:when test="${customer.customerSourceId == 1}"><fmt:message key='customer.select.self'/></c:when>
											<c:when test="${customer.customerSourceId == 2}"><fmt:message key='customer.select.companySet'/></c:when>
											<c:when test="${customer.customerSourceId == 3}"><fmt:message key='customer.select.market'/></c:when>
											<c:otherwise><fmt:message key='customer.select.other'/></c:otherwise>
										</c:choose>
									</td>
									<td><fmt:formatDate value="${customer.predictContract}" pattern="yyyy-MM-dd"/></td>
									<td>￥<fmt:formatNumber value="${customer.predictCost}" pattern="###,###,###,###.##"/></td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.requireDesc) > 8}">${fn:substring(customer.requireDesc,0,8)}......</c:when>
											<c:otherwise>${customer.requireDesc}</c:otherwise>
										</c:choose> 
									</td>
									<td>${customer.saleStage}</td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.saleRecord.subject) > 8}">${fn:substring(customer.saleRecord.subject,0,8)}......</c:when>
											<c:otherwise>${customer.saleRecord.subject}</c:otherwise>
										</c:choose> 
									</td>
								</tr>
								</c:forEach>
								<c:forEach var="customer" items="${user.customers}" begin="1" varStatus="c">
								<tr>
									<td>${c.count + 1}</td>
									<td>${customer.customerName}</td>
									<td>
										<c:choose>
											<c:when test="${customer.customerSourceId == 1}"><fmt:message key='customer.select.self'/></c:when>
											<c:when test="${customer.customerSourceId == 2}"><fmt:message key='customer.select.companySet'/></c:when>
											<c:when test="${customer.customerSourceId == 3}"><fmt:message key='customer.select.market'/></c:when>
											<c:otherwise><fmt:message key='customer.select.other'/></c:otherwise>
										</c:choose>
									</td>
									<td><fmt:formatDate value="${customer.predictContract}" pattern="yyyy-MM-dd"/></td>
									<td>￥<fmt:formatNumber value="${customer.predictCost}" pattern="###,###,###,###.##"/></td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.requireDesc) > 8}">${fn:substring(customer.requireDesc,0,8)}......</c:when>
											<c:otherwise>${customer.requireDesc}</c:otherwise>
										</c:choose> 
									</td>
									<td>${customer.saleStage}</td>
									<td>
										<c:choose>
											<c:when test="${fn:length(customer.saleRecord.subject) > 8}">${fn:substring(customer.saleRecord.subject,0,8)}......</c:when>
											<c:otherwise>${customer.saleRecord.subject}</c:otherwise>
										</c:choose> 
									</td>
								</tr>
								</c:forEach>
								<tr>
									<td class="title5">小计：</td>
									<td class="title5">客户数量：${fn:length(user.customers)}个</td>
									<td class="title5" colspan="6">预测签约金额：￥<fmt:formatNumber value="${user.salePeriodReport.predictCost}" pattern="###,###,###,###.##"/></td>
								</tr>
								</c:forEach>
								<tr>
									<td class="title3" colspan="2" align="center">合计：</td>
									<td class="title3">客户数量：${department.smallTotalSalePeriodReport.customerNum}个</td>
									<td class="title3" colspan="3">预测签约金额：￥<fmt:formatNumber value="${department.smallTotalSalePeriodReport.predictCost}" pattern="###,###,###,###.##"/></td>
									<td colspan="3"></td>
								</tr>
								</c:forEach>
								<tr>
									<td class="title4" colspan="3" style="color: red;" align="center">合计：</td>
									<td class="title4" style="color: red;">客户数量：${salePeriodReport4['totalSalePeriodReport'].customerNum}个</td>
									<td class="title4" colspan="3" style="color: red;">预测签约金额：￥<fmt:formatNumber value="${salePeriodReport4['totalSalePeriodReport'].predictCost}" pattern="###,###,###,###.##"/></td>
									<td colspan="3"></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan="3"></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>
</body>
</html>